******************************************************************
***   Programmer: C. Deal                             
***   Date Created: March 6, 2024
***   Replication File for Gonzales and Deal (2022) JAMA
******************************************************************

*Tell Stata where your data are located and any files should be saved

*Change to directory of replication folder:
global path "C:\Users\cdeal\Box\New Box Folder for RAs\Cameron Deal\Completed Projects\Transgender Youth Health Disparities\replication"
cd "$path"

*Open your data for analysis
use "$path\2017_2019_yrbs_states", clear

***Primary Grouping Variables

* Alternate method- best delineation between inclusion in study and non-inclusion
gen included1=0
replace included1 = 1 if qntransgender!=.
label define included1_label 0 "Not Included" 1 "Included"
label values included1 included1_label
label var included1 "Included in Study"
tab included1

*Transgender categories 
gen gendercat = .
replace gendercat = 1 if qtransgender=="1"
replace gendercat = 2 if qtransgender=="2"
replace gendercat = 3 if qtransgender=="3"
label define gendercat 1 "Cisgender" 2 "Transgender" 3 "Gender Questioning"
label values gendercat gendercat
label var gendercat "Gender Minority Status"
tab gendercat

*Strictly Transgender
gen transgender = .
replace transgender = 1 if qntransgender==1
replace transgender = 0 if qtransgender=="1"
label define transgender 0 "Cisgender" 1 "Transgender" 
label values transgender transgender
label var transgender "Transgender Status"
tab transgender

*Strictly Genderqueer
gen genderquest = .
replace genderquest = 1 if qtransgender=="3"
replace genderquest = 0 if qtransgender=="1" 
label define genderquest 0 "Non Gender Questioning" 1 "Gender Questioning" 
label values genderquest genderquest
label var genderquest "Gender Questioning Status"
tab genderquest

*Sensitivity Analysis
gen sens_gendercat = .
replace sens_gendercat = 1 if qtransgender=="1"
replace sens_gendercat = 2 if qtransgender=="2"
replace sens_gendercat = 3 if qtransgender=="3"
replace sens_gendercat = 4 if qtransgender=="4"
label define sens_gendercat 1 "Cisgender" 2 "Transgender" 3 "Gender Questioning" 4 "DK What Q is asking"
label values sens_gendercat sens_gendercat
label var sens_gendercat "Gender Minority Status"
tab sens_gendercat

*Sex- irrelevant to transgender status, unsure whether sex assigned at birth or gender identity
gen male = 0
replace male = 1 if sex==2
replace male = 2 if sex==. & included1==1

gen female = 0
replace female = 1 if sex==1

*Transgender Categories- unable to differentiate further because can't determine whether answers to sex question represent sex assigned at birth or gender identity

*Age Categories
gen agecat1 = .
replace agecat1 = 1 if age>=1 & age<=3
replace agecat1 = 2 if age>=4 & age<=5
replace agecat1 = 3 if age>=6 & age<=7
replace agecat1 = 4 if age==. & included1==1
label define agecat1 1 "12 and under to 14" 2 "15 to 16" 3 "17 to 18 and older" 4 "Missing"
label values agecat1 agecat1
label var agecat1 "Age Categories"
tab agecat1

*Grade Level is already found in the original dataset as grade

*Race is already found in the original dataset as race4
replace race4 = 5 if race4==. & included1==1

*Year
gen year_factor=.
replace year_factor = 0 if year==2017
replace year_factor = 1 if year==2019
tab year_factor

*Destring sitename
encode sitename, gen(sitename_factor)
tab sitename_factor

*Sexuality Binary (LGBT+ or not)
destring q66, generate(sexualitynum)
gen sexualitybin = .
replace sexualitybin = 0 if sexualitynum==1  
replace sexualitybin = 1 if sexpart2==3 & sexualitynum==1
replace sexualitybin = 1 if sexualitynum>=2 & sexualitynum<=4
label define sexualitybin_label 0 "Heterosexual" 1 "LGBT+"
label values sexualitybin sexualitybin_label
label var sexualitybin "LGBT+ or Not"
tab sexualitybin

*Sexual Orientation Categories
gen sexualitycat=.
replace sexualitycat = 1 if sexualitynum==1 
replace sexualitycat = 2 if sexualitynum==2 
replace sexualitycat = 3 if sexualitynum==3
replace sexualitycat = 4 if sexualitynum==4  
replace sexualitycat = 5 if sexualitynum==.
label define sexualitycat_label 1 "Heterosexual" 2 "Lesbian or Gay" 3 "Bisexual" 4 "Unsure" 5 "Missing"
label values sexualitycat sexualitycat_label
label var sexualitycat "Sexual Orientation Categories"
tab sexualitycat

*Weight- The analytic weight variable is labeled weight

***Health Outcomes

**Mental Health

*Considered Suicide
gen considered_suicide=.
replace considered_suicide = 1 if q26=="1"
replace considered_suicide = 0 if q26=="2"
tab considered_suicide

*Attempted Suicide
gen attempted_suicide=.
replace attempted_suicide = 1 if qn28==1
replace attempted_suicide = 0 if qn28==2
tab attempted_suicide

*Planned Suicide
gen planned_suicide=.
replace planned_suicide = 1 if q27=="1"
replace planned_suicide = 0 if q27=="2"
tab planned_suicide

*Sad or Hopeless
gen sad_hopeless=.
replace sad_hopeless = 1 if q25=="1"
replace sad_hopeless = 0 if q25=="2"
tab sad_hopeless

*Suicide attempt that required medical treatment
gen suicide_treatment=.
replace suicide_treatment = 1 if qn29==1
replace suicide_treatment = 0 if qn29==2
tab suicide_treatment


**Aggression

*Physical Fight
gen fight=.
replace fight = 1 if qn17==1
replace fight = 0 if qn17==2
tab fight

*Carried a Weapon
gen weapon=.
replace weapon = 1 if qn12==1
replace weapon = 0 if qn12==2
tab weapon

**Risky Sex Behaviors

*Sexually Active
gen sex_active=.
replace sex_active = 1 if qn61==1
replace sex_active = 0 if qn61==2
tab sex_active 

*Had sex with 4 or more partners
gen sex_4=.
replace sex_4 = 1 if qn60==1
replace sex_4 = 0 if qn60==2
tab sex_4

*Substances before sex
gen substance_sex=.
replace substance_sex = 1 if qn62==1
replace substance_sex = 0 if qn62==2
tab substance_sex

*Unprotected sex
gen unprotected_sex=.
replace unprotected_sex = 1 if q64=="2"
replace unprotected_sex = 0 if q64=="1" | q64=="3" | q64=="4" | q64=="5" | q64=="6" | q64=="7" | q64=="8" 
tab unprotected_sex


**Substance Use

*Cigarette smoking
gen cigarette=.
replace cigarette = 1 if q30=="1"
replace cigarette = 0 if q30=="2"
tab cigarette

*Alcohol Use
gen alcohol=.
replace alcohol = 1 if qn41==1
replace alcohol = 0 if qn41==2
tab alcohol

*Binge Drinking
gen binge_drinking=.
replace binge_drinking = 1 if qn42==1
replace binge_drinking = 0 if qn42==2
tab binge_drinking

*Marijuana
gen marijuana=.
replace marijuana = 1 if qn47==1
replace marijuana = 0 if qn47==2
tab marijuana

*Cocaine
gen cocaine=.
replace cocaine = 1 if qn50==1
replace cocaine = 0 if qn50==2
tab cocaine

*Smokeless Tobacco
gen no_smoke_tobacco=.
replace no_smoke_tobacco = 1 if qn37==1
replace no_smoke_tobacco = 0 if qn37==2
tab no_smoke_tobacco

*Cigarillos, Cigars, etc.
gen alt_smoking=.
replace alt_smoking = 1 if qn38==1
replace alt_smoking = 0 if qn38==2
tab alt_smoking

*Vaping
gen vape=.
replace vape = 1 if qn35==1
replace vape = 0 if qn35==2
tab vape

*Tobacco Binary
gen tobacco_bin=.
replace tobacco_bin = 1 if qntb4==1 
replace tobacco_bin = 0 if qntb4==2
tab tobacco_bin

*Hallucinogens
gen hallucin=.
replace hallucin = 1 if qnhallucdrug==1 
replace hallucin = 0 if qnhallucdrug==2
tab hallucin

*Heroin
gen heroin=.
replace heroin = 1 if qncurrentheroin==1 
replace heroin = 0 if qncurrentheroin==2
tab heroin

*Methamphetamines- sample too small for prevalence
gen meth=.
replace meth = 1 if qncurrentmeth==1 
replace meth = 0 if qncurrentmeth==2
tab meth

*Unprescribed drugs
gen unpresc=.
replace unpresc = 1 if qnprescription30d==1 
replace unpresc = 0 if qnprescription30d==2
tab unpresc


**Bullying

*Been bullied at school
gen bully_school=.
replace bully_school = 1 if qn23==1 
replace bully_school = 0 if qn23==2
tab bully_school

*Been bullied electronically
gen bully_elec=.
replace bully_elec = 1 if qn24==1 
replace bully_elec = 0 if qn24==2
tab bully_elec

*Bullied for weight
gen bully_weight=.
replace bully_weight = 1 if qnbullyweight==1 
replace bully_weight = 0 if qnbullyweight==2
tab bully_weight

*Bullied for gender
gen bully_gender=.
replace bully_gender = 1 if qnbullygender==1 
replace bully_gender = 0 if qnbullygender==2
tab bully_gender

*Bullied for Sexuality
gen bully_sexuality=.
replace bully_sexuality = 1 if qnbullygay==1 
replace bully_sexuality = 0 if qnbullygay==2
tab bully_sexuality


**Peer/Sexual Violence

*Forced to have sexual intercourse
gen forced_sex=.
replace forced_sex = 1 if qn19==1 
replace forced_sex = 0 if qn19==2
tab forced_sex

*Physical Dating Violence
gen phys_dv=.
replace phys_dv = 1 if qn22==1 
replace phys_dv = 0 if qn22==2
tab phys_dv

*Sexual Dating violence
gen sex_dv=.
replace sex_dv = 1 if qn21==1 
replace sex_dv = 0 if qn21==2
tab sex_dv

*****************************************
*** Identify Sample
*****************************************
*Adjust weights for pooled estimation (divide by the number of years)
gen one_year=0 
replace one_year=1 if sitename=="Delaware (DE)" | sitename=="Florida (FL)" | sitename=="Nevada (NV)" | sitename=="New Jersey (NJ)" | sitename=="New York (NY)" | sitename=="Pennsylvania (PA)" | sitename=="Virginia (VA)" 
gen weight_sample=.
replace weight_sample=weight if one_year==1
replace weight_sample=(weight/2) if one_year==0

*Tell Stata you are using survey weights
svyset psu [pweight=weight_sample], strata(stratum)
set more off, perm





***************************************************************************
***********   			     ANALYSIS    			     ******************
***************************************************************************
*Descriptive Statistics with `tabout' command

*Raw prevalence
tab gendercat

*Weighted Estimates
svy, subpop(if included1==1): tab gendercat, col

*Proper Prevalence
svy, subpop(if included1==1): tabulate gendercat, count format(%14.3gc)

***************************************************************************
***********   			     TABLE 1    			     ******************
***************************************************************************

*Actual Descriptive Statistics with tabout commands
*Transgender and Gender Questioning
foreach var in agecat1 grade race4 sex sexualitycat {
tabout `var' gendercat using descriptives.xls if included1==1, svy cell(col se) stats(chi2) nlab(count) f(4) sebnone append
}



***************************************************************************
***********   			     TABLE 2    			     ******************
***************************************************************************
foreach var in considered_suicide planned_suicide attempted_suicide suicide_treatment sad_hopeless fight weapon sex_active sex_4 unprotected_sex substance_sex cigarette no_smoke_tobacco alt_smoking vape tobacco_bin alcohol binge_drinking marijuana cocaine hallucin heroin unpresc bully_elec bully_school bully_weight bully_gender bully_sexuality forced_sex phys_dv sex_dv {
tabout `var' gendercat using healthdescriptives.xls if included1==1, svy cell(col se) stats(chi2) nlab(count) f(4) sebnone append
}

global covariates "i.agecat1 i.race4 i.sex i.sitename_factor i.sexualitycat"
global covariates_nostates "i.agecat1 i.race4 i.sex i.sexualitycat"

log using apr_results.smcl
*New Analysis (with wider confidence intervals):
*Adjusted Prevalence Ratios (Using Poission robust method):
foreach var in considered_suicide planned_suicide attempted_suicide suicide_treatment sad_hopeless sex_active sex_4 unprotected_sex substance_sex vape tobacco_bin alcohol binge_drinking marijuana cocaine bully_elec bully_school forced_sex phys_dv sex_dv {
 svy, subpop(if included1==1): glm `var' i.gendercat $covariates i.year_factor, fam(poisson) link(log) eform level(99.75)
}
log close








